package com.library.controller;


import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.library.entity.Book;
import com.library.entity.BorrowInfo;
import com.library.entity.Reader;
import com.library.service.IBookService;
import com.library.service.IBorrowInfoService;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 至尊伍人组
 * @since 2018-09-30
 */
@Controller
public class BorrowInfoController {
	
	@Autowired
	private IBorrowInfoService ibs;
	
	@Autowired
	private IBookService book;
	
	@RequestMapping("/borrow")
	public String borrow(@RequestParam int bookId,Model model,@RequestParam int time,HttpSession session) {
		//插入借书信息
		Reader reader = (Reader)session.getAttribute("reader");
		Integer readerId = reader.getReaderId();
		BorrowInfo borrowInfo = new BorrowInfo();
		borrowInfo.setBookId(bookId);
		borrowInfo.setBorrowDate(new Date());
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DAY_OF_YEAR, time);
		Date date = calendar.getTime();
		borrowInfo.setEndDate(date);
		borrowInfo.setReaderId(String.valueOf(readerId));
		borrowInfo.setBorrowTime(1);
		ibs.insert(borrowInfo);
		
		//改变书籍信息
		EntityWrapper<Book> entityWrapper = new EntityWrapper<Book>();
		entityWrapper.eq("bookId", bookId);
		Book one = book.selectOne(entityWrapper);
		if(one.getCurrentCount()>0) {
		one.setCurrentCount(one.getCurrentCount()-1);
		}
		book.update(one, entityWrapper);
		return "forward:reader/books/1";
	}
	
	@RequestMapping("/bookreturn")
	public String bookReturn(Model model,@RequestParam String borrowId,@RequestParam String isbn) {
		//删除借书记录
		int id = Integer.valueOf(borrowId);
		ibs.deleteById(id);
		
		//增加书本数量
		EntityWrapper<Book> wrapper = new EntityWrapper<Book>();
		wrapper.eq("isbn", isbn);
		Book one = book.selectOne(wrapper);
		one.setCurrentCount(one.getCurrentCount()+1);
		return "reader/readerborrow";
	}
	
	
	@RequestMapping("/borrowagain")
	@ResponseBody
	public Map<String,Object> borrowAgain(Model model,@RequestParam String borrowId,HttpSession session,HttpServletResponse response) throws IOException {
		//增加借书时间
		Map<String,Object> map = new HashMap<String,Object>();
		int id = Integer.valueOf(borrowId);
		EntityWrapper<BorrowInfo> entityWrapper = new EntityWrapper<BorrowInfo>();
		entityWrapper.eq("borrowId", id);
		BorrowInfo one = ibs.selectOne(entityWrapper);
		if(one.getBorrowTime()>1) {
			map.put("result", false);
		}else {
		one.setBorrowTime(2);
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(one.getEndDate());
		calendar.add(Calendar.DAY_OF_YEAR, 30);
		Date date = calendar.getTime();
		one.setEndDate(date);
		ibs.update(one, entityWrapper);
		map.put("result", true);
		}
		return map;
	}
}

